#include "CustomChart.h"
#include "ui_CustomChart.h"

CustomChart::CustomChart(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::CustomChart)
{
    ui->setupUi(this);
    this->initValue();
}

CustomChart::~CustomChart()
{
    delete ui;
}

void CustomChart::initValue()
{
    // 设置背景色
    ui->widget->setBackground(QBrush(BACKGROUND_COLOR));

    // 设置坐标轴颜色
    ui->widget->xAxis->setTickLabelColor(TEXT_COLOR);
    ui->widget->yAxis->setTickLabelColor(TEXT_COLOR);

    //设置1为固定的一个刻度
    ui->widget->xAxis->setAutoTickStep(false);
    ui->widget->xAxis->setTickStep(1);
    ui->widget->xAxis->setSubTickCount(1);
    ui->widget->yAxis->setAutoTickStep(false);
    ui->widget->yAxis->setTickStep(1);
    ui->widget->yAxis->setSubTickCount(1);

    //设置画笔
    QPen paintPen;
    paintPen.setColor(LIN_COLOR);
    paintPen.setWidth(3);
    ui->widget->addGraph();
    ui->widget->graph(DATA_POINT)->setPen(paintPen);
    ui->widget->graph(DATA_POINT)->setLineStyle(QCPGraph::LineStyle::lsLine);

    //设置第一个点
    ui->widget->addGraph();
    ui->widget->graph(SMALL_POINT)->setPen(QPen(Qt::darkRed));
    ui->widget->graph(SMALL_POINT)->setLineStyle(QCPGraph::lsNone);
    ui->widget->graph(SMALL_POINT)->setScatterStyle(QCPScatterStyle::ssDisc);

    //设置第二个点
    ui->widget->addGraph();
    ui->widget->graph(SMALL_POINT)->setPen(QPen(Qt::darkRed));
    ui->widget->graph(SMALL_POINT)->setLineStyle(QCPGraph::lsNone);
    ui->widget->graph(SMALL_POINT)->setScatterStyle(QCPScatterStyle(QCPScatterStyle::ssDisc, 12));

    // 注册监听事件
    ui->widget->installEventFilter(this);

    ui->widget->yAxis->grid()->setPen(QPen(Qt::NoPen));
    ui->widget->xAxis->grid()->setPen(QPen(Qt::NoPen)); //设置x轴绘制网格线颜色的笔（大刻度位置对应的网格线）Qt::NoPen表示没有画笔颜色，则x轴大刻度位置的网格线看不到
    //ui->widget->yAxis->grid()->setPen(QPen(QColor(227, 227, 227), Qt::SolidLine));  //设置y轴大刻度线位置对应网格线的颜色，Qt::SolidLine表示网格线为实线

}

void CustomChart::setControlData(const CoorData &coorData)
{
    pointData = coorData;
    // ui->widget->graph(DATA_POINT)->setData(coorData.dataX, coorData.dataY);
    ui->widget->graph(DATA_POINT)->setData(coorData.dataX, coorData.dataY);
    ui->widget->replot();
}

void CustomChart::setZoom(bool canZoom)
{
    if(canZoom){
        //可放大缩小
        ui->widget->setInteractions(QCP::iRangeDrag | QCP::iRangeZoom);
    }else{

    }
}

void CustomChart::setXRange(double lower, double upper)
{
    //设置范围
    ui->widget->xAxis->setRange(lower, upper);
    rangs.xmax = upper;
    rangs.xmin = lower;

}

void CustomChart::setYRange(double lower, double upper)
{
    ui->widget->yAxis->setRange(lower, upper);
    rangs.ymax = upper;
    rangs.ymin = lower;
}

void CustomChart::setStep(bool isx,double step)
{
    if(isx){
        ui->widget->xAxis->setTickStep(step);
        ui->widget->xAxis->setAutoTickStep(true);
    }else{
        ui->widget->yAxis->setTickStep(step);
        ui->widget->yAxis->setAutoTickStep(true);
    }
}

void CustomChart::setTimeCoorX()
{
    ui->widget->xAxis->setTickLabelType(QCPAxis::ltDateTime);
    // ui->widget->xAxis->setDateTimeFormat("hh:mm:ss");
    ui->widget->xAxis->setDateTimeFormat("mm:ss");
    // ui->widget->xAxis->setDateTimeSpec(Qt::LocalTime);
    //ui->widget->xAxis->setAutoTickStep(false);
    ui->widget->yAxis->setAutoTickStep(true);
    // ui->widget->xAxis->setTickStep(2);
    // ui->widget->xAxis->setTickStep(60);
    ui->widget->xAxis->setAutoTickStep(true);
    ui->widget->axisRect()->setupFullAxesBox();
}

void CustomChart::updateCoor()
{
    // ui->widget->rescaleAxes();
    ui->widget->replot();
}

void CustomChart::appendValue(double x, double y)
{
    ui->widget->graph(DATA_POINT)->addData(x, y);
}

void CustomChart::removeValue(double key)
{
    ui->widget->graph(DATA_POINT)->removeDataBefore(key);
}

void CustomChart::setPenColor(QColor color)
{
    //设置画笔
    QPen paintPen;
    paintPen.setColor(color);
    paintPen.setWidth(1);
    ui->widget->addGraph();
    paintPen.setStyle(Qt::PenStyle::CustomDashLine);
    ui->widget->graph(DATA_POINT)->setPen(paintPen);

}

void CustomChart::setShowPoint(int ok)
{
    ui->widget->graph(DATA_POINT)->setScatterStyle(QCPScatterStyle(QCPScatterStyle::ScatterShape::ssDisc,ok));
}

void CustomChart::setLineStyle(int index)
{
    ui->widget->graph(DATA_POINT)->setLineStyle(QCPGraph::LineStyle(index)) ;
}


/*********************     鼠标移动    ********************/
bool CustomChart::eventFilter(QObject *watched, QEvent *event)
{
    if(watched == ui->widget && event->type() == QEvent::MouseMove) {
        QMouseEvent *mouseEvent = static_cast<QMouseEvent*>(event);
        int xPos = mouseEvent->pos().x();
        int yPos = mouseEvent->pos().y();

        float xValue = ui->widget->xAxis->pixelToCoord(xPos);
        float yValue = ui->widget->yAxis->pixelToCoord(yPos);

        QPoint pointMatch = matchPoint(QPoint(xValue, yValue));

        CoorData bigPointData;
        bigPointData.dataX.append(pointMatch.x());
        bigPointData.dataY.append(pointMatch.y());

        ui->widget->graph(BIG_POINT)->setData(bigPointData.dataX, bigPointData.dataY);
        ui->widget->replot();
    }

    return QWidget::eventFilter(watched, event);
}


QPoint CustomChart::matchPoint(QPoint point)
{
    /******************************************
     * 思路：
     * 鼠标当前的点与实际坐标相比较，
     * 离哪个点比较近，返回当前比较近的点的坐标
     * *****************************************/

    QList<QPoint> pointList;
    QList<float> compareompareList;
    for(int i = 0; i < pointData.dataX.size(); i ++) {
        float diffValue = point.x() - pointData.dataX.at(i);
        if(diffValue > -1 && diffValue < 1) {
            pointList.append(QPoint(pointData.dataX.at(i), pointData.dataY.at(i)));

            compareompareList.append(std::abs(pointData.dataX.at(i) - point.x()));
        }
    }

    if(compareompareList.size() != 0) {
        int minIndex = 0;
        float minValue = compareompareList.at(0);
        for(int i = 0; i < compareompareList.size(); i ++) {
            if(compareompareList.at(i) < minValue) {
                minValue = compareompareList.at(i);
                minIndex = i;
            }
        }
        if (minIndex < pointList.size()) {
            return pointList.at(minIndex);
        }
    }

    return QPoint(0, 0);
}
